﻿using System;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using Item;
using Item.Buff;
using ItemComponent;
using LitJson;

namespace ILRuntime
{
    public class MyItemComponentAdaptor : CrossBindingAdaptor
    {
        //属性的内置接口
        static CrossBindingMethodInfo<System.Boolean> mSetActive = new CrossBindingMethodInfo<System.Boolean>("set_Active");
        static CrossBindingFunctionInfo<System.Boolean> mGetActive = new CrossBindingFunctionInfo<System.Boolean>("get_Active");

        static CrossBindingFunctionInfo<System.String> mGetDes = new CrossBindingFunctionInfo<System.String>("GetDes");
        static CrossBindingMethodInfo<STBuffEffectData> mOnBuffChange = new CrossBindingMethodInfo<STBuffEffectData>("OnBuffChange");
        static CrossBindingMethodInfo<JsonData,ItemBase> mInitComponent = new CrossBindingMethodInfo<JsonData, ItemBase>("InitComponent");

        public override Type BaseCLRType => typeof(IMyItemComponent);

        public override Type AdaptorType => typeof(Adaptor);

        public override object CreateCLRInstance(Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            return new Adaptor(appdomain, instance);
        }

        public class Adaptor : IMyItemComponent,CrossBindingAdaptorType
        {
            ILTypeInstance instance;
            ILRuntime.Runtime.Enviorment.AppDomain appdomain;

            public bool Active { get => mGetActive.Invoke(instance); set =>mSetActive.Invoke(instance,value); }

            public ILTypeInstance ILInstance => instance;

            public Adaptor() { }

            public Adaptor(Runtime.Enviorment.AppDomain appDomain,ILTypeInstance instance)
            {
                this.appdomain = appDomain;
                this.instance = instance;
            }

            public void InitComponent(JsonData data, ItemBase item)
            {
                mInitComponent.Invoke(instance, data, item);
            }

            public void OnBuffChange(STBuffEffectData buffEffectData)
            {
                mOnBuffChange.Invoke(instance, buffEffectData);
            }

            public string GetDes()
            {
                return mGetDes.Invoke(instance);
            }
        }
    }
}
